<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: version_upgrade.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * Installation template class
 *
 * @package Fusion Registry
 * @subpackage installer
 * @version $Id: version_upgrade.php 4 2008-08-03 01:43:03Z registry.fusion $
 */
if ( ! defined( 'IN_IPB' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
    exit();
}

class version_upgrade
{
    var $ipsclass;
    var $instagrade;
    var $this_version = '20007';
    var $upgrade_from = '10200';
    var $base_url     = '';
    var $mod_to_run   = '';


    /**
     * Generate the upgrades to happen string
     */
    function version_process()
    {
        $this->base_url  = "index.php?act=kit";

        if ( is_array( $this->instagrade->modules_to_run ) and count( $this->instagrade->modules_to_run ) )
        {
            $tmp = array_shift( $this->instagrade->modules_to_run );

            $this->mod_to_run = implode( ', ', $this->instagrade->modules_to_run );
        }

        if ( ! $this->mod_to_run )
        {
            $this->mod_to_run = 'None';
        }
    }


    /**
     * Oh how we love to upgrade!
     */
    function auto_run()
    {
        switch( $this->ipsclass->input['kitact'] )
        {
            case 'sql':
                $this->update_sql();
                break;

            case 'images':
                $this->convert_images();
                break;

            case 'post_sql':
                $this->post_sql();
                break;

            default:
                $this->upgrade_kit_intro();
                break;
        }
    }


    /**
     * Create our new tables
     */
    function update_sql()
    {
        require './instagrade/sql_importer.php';

        $sql_importer = new sql_importer();
        $sql_importer->ipsclass =& $this->ipsclass;
        $sql_importer->auto_run();

        $executed = 0;

        if ( file_exists( './upgrade_' . $this->instagrade->current_upgrade . '/mysql_updates.sql.php' ) )
        {
            $executed = $sql_importer->execute_sql_file('./upgrade_' . $this->instagrade->current_upgrade . '/mysql_updates.sql.php');
        }

        $this->instagrade->redirect( $this->base_url."&kitact=images", "SQL updates complete, $executed queries run...." );
    }


    /**
     * Migrate the images to the new schema
     */
    function convert_images()
    {
        $executed = 0;

        // Since we never stored time before for images,
        //   we are going to start with a fresh time.
        $time = time();

        // Grab our existing images
        $rs = $this->ipsclass->DB->query( "SELECT * FROM ".$this->ipsclass->vars['sql_tbl_prefix']."garage_images" );

        // Do it...
        while( $r = $this->ipsclass->DB->fetch_row($rs) )
        {
            $image   = 0;
            $ext     = strtolower( str_replace( ".", "", substr( $r['attach_file'], strrpos( $r['attach_file'], '.' ) ) ) );

            if ( in_array( $ext, array( 'gif', 'jpeg', 'jpg', 'png' ) ) )
            {
                $image = 1;
            }

            //-----------------------------------------
            // Only calculate filesize for local images
            //-----------------------------------------
            if ( preg_match( "/^http:\/\//i", $r['attach_location']) )
            {
                $filesize = 0;
            }
            else
            {
                $filesize = @filesize( ROOT_PATH.'uploads/'.$r['attach_location'] );
            }

            $db_string = $this->ipsclass->DB->compile_db_update_string( array( 'attach_ext'       => $ext,
                                                                               'attach_is_image'  => $image,
                                                                               'attach_date'      => $time,
                                                                               'attach_filesize'  => $filesize,
                                                                      )      );

            $this->ipsclass->DB->query( "UPDATE ".$this->ipsclass->vars['sql_tbl_prefix']."garage_images SET $db_string WHERE attach_id='".$r['attach_id']."'");

            $executed++;
        }

        $this->instagrade->redirect( $this->base_url."&kitact=post_sql", "Image schema migration complete, $executed queries run...." );
    }


    /**
     * Post SQL updates
     */
    function post_sql()
    {
        require './instagrade/sql_importer.php';

        $sql_importer = new sql_importer();
        $sql_importer->ipsclass =& $this->ipsclass;
        $sql_importer->auto_run();

        $executed = 0;

        if ( file_exists( './upgrade_' . $this->instagrade->current_upgrade . '/mysql_post_updates.sql.php' ) )
        {
            $executed = $sql_importer->execute_sql_file('./upgrade_' . $this->instagrade->current_upgrade . '/mysql_post_updates.sql.php');
        }

        $this->instagrade->redirect( "index.php?act=done", "SQL updates complete, $executed queries run...." );
    }


    /**
     * Print intro
     */
    function upgrade_kit_intro()
    {
        $this->ipsclass->template->print_header();

        $this->ipsclass->template->contents .= <<<HTML
This upgrade kit will upgrade you from <b>"{$this->instagrade->version_history[$this->upgrade_from]}"</b> to <b>"{$this->instagrade->version_history[$this->this_version]}"</b>
<br />
<br />
<div align='center'><a href="{$this->base_url}&kitact=sql" class='button' style='color: #003366'>Continue >></a></div>
<br />
<br />
<div align='center'>Upgrade kits still to run after this upgrade: {$this->mod_to_run}</div>
HTML;

        $this->ipsclass->template->output();
    }

}


?>